﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HXDNB.CMS.App_Code;
using HXDNB.DAL;
using HXDNB.Infrastructure;

namespace HXDNB.CMS.Controllers
{
    public class BuyOrderController : BaseController
    {
        public ActionResult List(DateTime? OrderDate, int Customer = -1, int Product = -1, int State = -1)
        {
            BuyParentBLL bll = new BuyParentBLL();
            var list = bll.GetList(OrderDate, Customer, Product, State);
            var pag = new Paging<BuyParentList>(list);
            ViewBag.PagList = pag;
            BuyOrderBLL OrderBLL = new BuyOrderBLL();
            ViewBag.OrderList = OrderBLL.GetInfoList(pag.DataList.Select(i => i.ID).Distinct().ToList());
            return View();
        }

        public ActionResult Info(int? id)
        {
            BuyOrderBLL bll = new BuyOrderBLL();
            var info = new BuyOrder();
            if (id != null)
            {
                info = bll.GetByKey((int)id);
            }
            return View(info);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Info(BuyOrder info)
        {
            BuyOrderBLL bll = new BuyOrderBLL();
            bool result = false;
            if (info.ID > 0)
            {               
                result = bll.update(info);               
            }
            else
            {
                info.State = (int)BuyOrderState.待出货;
                info.Createtime = DateTime.Now;
                info.ID = bll.insert(info);
                result = info.ID > 0;
            }
            return RedirectToAction("Info", new { id = info.ID, ParentID = info.ParentID, result = result ? "Y" : "N" });
        }

        public ActionResult ParentInfo(int? id)
        {
            BuyParentBLL bll = new BuyParentBLL();
            var info = new BuyParentOrder();
            if (id != null)
            {
                info = bll.GetByKey((int)id);
            }
            return View(info);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult ParentInfo(BuyParentOrder info)
        {
            BuyParentBLL bll = new BuyParentBLL();
            bool result = false;
            if (info.ID > 0)
            {
                result = bll.update(info);
            }
            else
            {
                info.State = (int)BuyPayState.未付款;
                info.CreateTime = DateTime.Now;
                info.ID = bll.insert(info);
                result = info.ID > 0;
            }
            return RedirectToAction("ParentInfo", new { id = info.ID, result = result ? "Y" : "N" });
        }

        [HttpPost]
        public string Delete(string id = "")
        {
            BuyParentBLL bll = new BuyParentBLL();
            BuyOrderBLL Orderbll = new BuyOrderBLL();
            bool result = true;
            foreach (string item in id.Split(',').Where(item => !string.IsNullOrWhiteSpace(item)))
            {
                List<BuyOrder> BuyOrders = Orderbll.GetInfoByParentID(int.Parse(item));
                foreach (BuyOrder Order in BuyOrders)
                {
                    result = DeleteSon(Order.ID) == true.ToString();
                }
                if (result)
                {
                    result = bll.Delete(int.Parse(item));
                }
                else
                {
                    break;
                }
            }
            return result.ToString();
        }
        [HttpPost]
        public string DeleteSon(int id = 0)
        {
            bool result = false;
            if (id > 0)
            {
                BuyOrderBLL bll = new BuyOrderBLL();
                BuyOrder Info = bll.GetByKey(id);
                if ((BuyOrderState)Info.State < BuyOrderState.已收货)
                {
                    result = bll.Delete(id);
                }
            }
            return result.ToString();
        }
        [HttpPost]
        public string SetPayState(int id)
        {
            bool result = false;
            if (id > 0)
            {
                BuyParentBLL bll = new BuyParentBLL();
                BuyParentOrder Info = bll.GetByKey(id);
                Info.State = (int)BuyPayState.已付款;
                result = bll.update(Info);
            }
            return result.ToString();
        }
        [HttpPost]
        public string SetState(int id)
        {
            bool result = false;
            if (id > 0)
            {
                BuyOrderBLL bll = new BuyOrderBLL();
                BuyOrder Info = bll.GetByKey(id);
                switch ((BuyOrderState)Info.State)
                {
                    case BuyOrderState.待出货:
                        Info.State = (int)BuyOrderState.已出货;
                        Info.ShipTime = DateTime.Now.Date;
                        break;
                    case BuyOrderState.已出货:
                        Info.State = (int)BuyOrderState.发货中;
                        Info.SendTime = DateTime.Now.Date;
                        break;
                    case BuyOrderState.发货中:
                        Info.State = (int)BuyOrderState.已收货;
                        Info.ReceivedTime = DateTime.Now.Date;
                        ProductBLL ProdBll = new ProductBLL();
                        ProdBll.AddStock((int)Info.ProductID, (int)Info.Num);
                        break;
                    default:
                        break;
                }
                result = bll.update(Info);
            }
            return result.ToString();
        }
    }
}
